<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-input placeholder="请输入用户名" v-model="query">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="seacher"
          ></el-button>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="add">添加</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table border :data="user" style="width: 100%">
        <el-table-column prop="id" label="#"> </el-table-column>
        <el-table-column prop="username" label="姓名"> </el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"> </el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column prop="" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="change(scope.row.id, scope.row.mg_state)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="edit(scope.row)"
              circle
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
              circle
            ></el-button>
            <el-button type="warning" icon="el-icon-s-tools" circle></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[3, 6, 9]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </template>
    <!-- 添加 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="用户名">
          <el-input v-model="addusers.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="addusers.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="addusers.email"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="addusers.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="adduser()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog title="修改用户" :visible.sync="edit_dialogFormVisible">
      <el-form>
        <el-form-item label="用户名">
          <el-input v-model="editusers.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editusers.email"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="editusers.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="qxedit">取 消</el-button>
        <el-button type="primary" @click="edituser()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { users, delusers, adduser, change, edit } from "@/http/http.js";

export default {
  name: "",
  components: {},
  props: [],
  data() {
    return {
      user: [],
      pagenum: 1,
      pagesize: 6,
      query: "",
      total: 0,
      dialogFormVisible: false,
      edit_dialogFormVisible: false,
      // 添加用户
      addusers: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      // 修改用户
      editusers: {
        id: "",
        username: "",
        email: "",
        mobile: "",
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getuser();
  },
  methods: {
    // 获取用户列表
    async getuser() {
      const { data: res } = await users({
        query: this.query,
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      });
      this.total = res.data.total;
      this.user = res.data.users;
    },
    handleSizeChange(size) {
      this.pagesize = size;
      this.getuser();
    },
    handleCurrentChange(num) {
      this.pagenum = num;
      this.getuser();
    },
    // 删除用户
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await delusers(`users/${id}`);
          console.log(res);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getuser();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 打开添加对话框
    add() {
      this.dialogFormVisible = true;
    },
    // 确认添加用户
    async adduser() {
      const { data: res } = await adduser(this.addusers);
      console.log(res);
      this.dialogFormVisible = false;
      this.getuser();
    },
    // 修改用户状态
    async change(id,statu) {
      const { data: res } = await change(`users/${id}/state/${statu}`);
      this.$message.success({
        message: res.meta.msg,
      });
    },
    // 搜索用户
    seacher() {
      this.getuser();
    },
    // 修改
    edit(data) {
      this.edit_dialogFormVisible = true;
      this.editusers = data;
    },
    // 确认修改
    async edituser() {
      const { data: res } = await edit(
        `/users/${this.editusers.id}`,
        this.editusers
      );
      console.log(res);
      this.$message.success({
        message: res.meta.msg,
      });
      this.edit_dialogFormVisible = false;
    },
    // 取消修改
    qxedit() {
      this.edit_dialogFormVisible = false;
      this.getuser();
    },
  },
};
</script>

<style lang='stylus' scoped></style>
